﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.NetClasses;
namespace Server
{
    class Server : Engine.NetClasses.BaseServer
    {
        bool autoReg = false;
        AccauntManager _manager;
        public Server()
            : base()
        {
            _manager = new AccauntManager();
        }
        public override void Update()
        {

            base.Update();
        }
        public override void ReadData(Engine.NetClasses.IAsyncClient client)
        {
            PacketsBilder[] pacets = client.GetFromClient();
            for (int i = 0; i < pacets.Length; i++)
            {
                PacketType type = (PacketType)Enum.Parse(typeof(PacketType), pacets[i].Header._ptype.ToString(), true);
                doAction(type, pacets[i], client);

            }
            base.ReadData(client);
        }

        private void doAction(PacketType _type, PacketsBilder pacets, IAsyncClient client)
        {
            switch (_type)
            {
                case PacketType.NullPacket:
                    break;
                case PacketType.Init:
                    break;
                case PacketType.LoginOk:
                    break;
                case PacketType.RequestLoginAuth:
                    RequestLoginAuth loginReqest = pacets.getStryct();
                    string login = new string(loginReqest.Login).Replace("\0", "");
                    char[] pas = _manager.GetPassword(loginReqest.Login);
                    bool LoginExist = true;
                    if (pas==null && autoReg)
                    {
                        _manager.AddInBase(loginReqest.Login, loginReqest.Password);
                        pas = loginReqest.Password;
                        LoginExist = true;
                    }
                    if (pas==null && !autoReg)
                    {
                         LoginExist = false;
                    }
                    string password = new string(loginReqest.Password).Replace("\0", "");
                    if (LoginExist)
                    {
                        if (password.Trim() == new string(pas).Replace("\0", "").Trim())
                        {
                            LoginOk lOk = (LoginOk)NetPacket.GetPacket(2);
                            Random rnd = new Random((int)((client.ID + ((ulong)Environment.TickCount / 5632)) * 256 / (65 - client.ID) * 659 / (45 + client.ID) - 1632 * client.ID + 8456 * (2 + client.ID)));
                            client.SessionKey = (ulong)(rnd.Next(0, int.MaxValue) + rnd.Next(0, int.MaxValue)) + (ulong)rnd.Next(int.MaxValue / 2, int.MaxValue);
                            byte[] tmp = BitConverter.GetBytes(client.SessionKey);
                            uint frist = BitConverter.ToUInt32(tmp, 0);
                            uint sec = BitConverter.ToUInt32(tmp, 4);
                            lOk.Frist = frist;
                            if (sec == 0)
                            {
                                lOk.Secondory = (uint)(rnd.Next(0, int.MaxValue) + rnd.Next(0, int.MaxValue)) + (uint)rnd.Next(int.MaxValue / 2, int.MaxValue);
                                byte[] tmps = new byte[8];

                                Array.Copy(BitConverter.GetBytes(frist), 0, tmps, 0, 4);
                                Array.Copy(BitConverter.GetBytes(lOk.Secondory), 0, tmps, 4, 4);
                                client.SessionKey = BitConverter.ToUInt64(tmps, 0);
                            }
                            else
                            {
                                lOk.Secondory = sec;
                            }
                            client.CR = new Engine.Helpers.BlowFish(BitConverter.GetBytes(client.SessionKey));
                            client.SetFromServer(new PacketsBilder(lOk));

                            _manager.SetOnline(loginReqest.Login);
                        }
                        else
                        {
                            LoginFail lfalse = (LoginFail)NetPacket.GetPacket(4);
                            lfalse.FailCode = 2;
                            client.SetFromServer(new PacketsBilder(lfalse));
                        }
                    }
                    else
                    {
                        LoginFail lfalse = (LoginFail)NetPacket.GetPacket(4);
                        lfalse.FailCode = 1;
                        client.SetFromServer(new PacketsBilder(lfalse));
                    }
                    break;
                case PacketType.LoginFail:
                    break;
                case PacketType.NetPing:
                    break;
                case PacketType.RequestServerList:
                    RequestServerList ServerList = pacets.getStryct();
                    ServersList[] list = ServerOptions.ServerList;
                    if (list != null)
                    {
                        ListServer l = (ListServer)NetPacket.GetPacket((byte)PacketType.ServerList);
                        l.FromArray(list);
                        client.SetFromServer(new PacketsBilder(l));
                    }

                    break;
                default:
                    break;
            }
        }
    }
}
